package com.yj.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.yj.model.Academy;
import com.yj.model.Admin;
import com.yj.model.Classes;
import com.yj.model.Course;
import com.yj.model.Sc;
import com.yj.model.Student;
import com.yj.model.Teacher;
import com.yj.utils.Model;

@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends BaseAction<Admin> {

	private static final long serialVersionUID = 1L;
	
	public String studentList() {
		if(session.get("admin") == null) {
			return ERROR;
		}
		request.put("allAcademy", academyService.query());
		return "studentList";
	}
	
	public String teacherList() {
		if(session.get("admin") == null) {
			return ERROR;
		}
		request.put("allAcademy", academyService.query());
		return "teacherList";
		
	}
	
	public String courseList() {
		if(session.get("admin") == null) {
			return ERROR;
		}
		request.put("allCourse", courseService.query());
		return "courseList";
		
	}

	public String scList() {
		if(session.get("admin") == null) {
			return ERROR;
		}
		request.put("allSc", scService.query());
		return "scList";
		
	}
	
	
	public String addCourseForClasses() {
		if(session.get("admin") == null) {
			return ERROR;
		}
		return "addCourseForClasses";
	}
	
	public String insertToClasses() {
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer classesId = Integer.parseInt(hsRequest.getParameter("classesId"));
		Integer courseId = Integer.parseInt(hsRequest.getParameter("courseId"));
		Classes classes = classesService.get(classesId);
		Course course = courseService.get(courseId);
		for(Student student:classes.getStudents()) {
			Sc sc = new Sc(course,student);
			scService.save(sc);
		}
		course.setSelected(course.getSelected() + classes.getStudents().size());
		courseService.update(course);
		request.put("message", "添加成功!");
		request.put("allSc", scService.query());
		return "insertToClasses";
	}
	
	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	//获取学院集合
	public String getAllAcademy() {
		System.out.println("getAllAcademy");
		List<Academy> academy = academyService.query();
		List<Model> allAcademy = new ArrayList<Model>();
		for(Academy a:academy) {
			Model model = new Model(a.getId(),a.getName());
			allAcademy.add(model);
		}
		Gson json = new Gson();
		result = json.toJson(allAcademy);
		return SUCCESS;
	}
	
	//获取班级集合
	public String getAllClasses() {
		System.out.println("getAllClasses");
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer aid = Integer.parseInt(hsRequest.getParameter("aid"));
		List<Classes> allClasses = classesService.queryByAcademyId(aid);
		List<Model> classes = new ArrayList<Model>();
		for(Classes c:allClasses) {
			Model model = new Model(c.getId(), c.getName());
			classes.add(model);
		}
		Gson json = new Gson();
		result = json.toJson(classes);
		return SUCCESS;
	}
	
	//获取课程集合
	public String getAllCourse() {
		System.out.println("getAllCourse");
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer aid = Integer.parseInt(hsRequest.getParameter("aid"));
		List<Teacher> teachers = teacherService.queryByAcademyId(aid);
		List<Model> courses = new ArrayList<Model>();
		for(Teacher teacher:teachers) {
			for(Course c:teacher.getCourses()) {
				Model model = new Model(c.getId(),c.getName() + "[" + c.getType() + "]" + "(" + teacher.getName() +")");
				courses.add(model);
			}
		}
		Gson json = new Gson();
		result = json.toJson(courses);
		return SUCCESS;
	}
}
